﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using aerowan.pilot_client.Simulator;

namespace aerowan.pilot_client
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainWindow_Loaded);
            Closing += new System.ComponentModel.CancelEventHandler(MainWindow_Closing);
        }

        void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            FlightSimulator.Instance.Close();
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            if (!FlightSimulator.Instance.Connect())
            {
                MessageBox.Show("Cannot connect to your flight simulator. Please check it is running and restart AeroWAN pilot client.");
                Environment.Exit(1);
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (ApplicationContext.Instance.Connected == true)
            {
                ApplicationContext.Instance.Disconnect();
            }
            else if(ApplicationContext.Instance.Connected == false)
            {
                if (String.IsNullOrWhiteSpace(textBox1.Text))
                {
                    MessageBox.Show("You must input your Account ID.");
                    return;
                }
                if (String.IsNullOrWhiteSpace(textBox2.Password))
                {
                    MessageBox.Show("You must input your Password.");
                    return;
                }
                if (String.IsNullOrWhiteSpace(textBox3.Text))
                {
                    MessageBox.Show("You must input a call sign.");
                    return;
                }
                int accountId;
                if (!int.TryParse(textBox1.Text, out accountId))
                {
                    MessageBox.Show("Account ID: invalid VID.");
                    return;
                }
                ApplicationContext.Instance.AuthAccountID = accountId;
                ApplicationContext.Instance.AuthPassword = textBox2.Password;
                ApplicationContext.Instance.AuthCallsign = textBox3.Text;
                ApplicationContext.Instance.Connect();
            }
        }
    }
}
